//
//  WH_NewOneCell.m
//  App_6.23
//
//  Created by wanghao on 16/7/22.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_NewOneCell.h"
#import "WH_Home.h"
#import "TimeModel.h"
@interface WH_NewOneCell ()


@property (nonatomic, strong) UIImageView *picImage;
@property (nonatomic, strong) UILabel *picLabel;
@property (nonatomic, strong) UILabel *qihaoLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) NSTimer *productTimer;

@property (nonatomic, weak)   id           m_data;
@property (nonatomic, weak)   NSIndexPath *m_tmpIndexPath;


@end
@implementation WH_NewOneCell
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubs];
        [self registerNSNotificationCenter];

    }
    
    return self;
}

-(void)createSubs{
    
    self.picImage = [[UIImageView alloc] init];
    self.picImage.backgroundColor = ARCDOM_4;
    [self.contentView addSubview:_picImage];
    [self.picImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(@0);
        make.width.height.mas_equalTo(@(SCREEN_WIDTH/2));
        
    }];
    
    self.picLabel = [UILabel new];
    self.picLabel.text = @"Apple iPhone6s 16G 颜色随机";
    self.picLabel.font = [UIFont systemFontOfSize:15.0];
    self.picLabel.numberOfLines = 2;
    [self.contentView addSubview:_picLabel];
    [self.picLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@5);
        make.top.equalTo(self.picImage.mas_bottom);
        make.right.mas_equalTo(@-5);
        make.height.mas_equalTo(36);
    }];
    
    self.qihaoLabel = [UILabel new];
    _qihaoLabel.text = @"期号: 30231332424";
    _qihaoLabel.font = [UIFont systemFontOfSize:11];
    _qihaoLabel.alpha = .6;
    [self.contentView addSubview:_qihaoLabel];
    [self.qihaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.picLabel);
        make.top.equalTo(self.picLabel.mas_bottom);
        make.height.mas_equalTo(@(20));
        make.right.mas_equalTo(@-10);
    }];
    UILabel *labX = [UILabel new];
    labX.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.contentView addSubview:labX];
    [labX mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(@0);
        make.bottom.mas_equalTo(@-1);
        make.height.mas_equalTo(@1);
    }];
    UILabel *labR = [UILabel new];
    labR.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.contentView addSubview:labR];
    [labR mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.mas_equalTo(@0);
        make.right.mas_equalTo(@0);
        make.width.mas_equalTo(@1);
    }];
    
    [self createWill];
    [self createDid];

    
}
-(void)createWill{
    
    UILabel *lab = [UILabel new];
    lab.text = @"即将揭晓";
    lab.tag = 981;
    lab.textColor = [UIColor colorWithRed:0.78 green:0.14 blue:0.27 alpha:1];
    [self.contentView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@5);
        make.top.equalTo(self.qihaoLabel.mas_bottom);
        make.right.height.equalTo(self.qihaoLabel);
        
    }];
    
    self.timeLabel = [UILabel new];
    //_timeLabel.text = @"08 : 48 : 59";
    _timeLabel.textColor = [UIColor colorWithRed:0.78 green:0.14 blue:0.27 alpha:1];
    _timeLabel.font = [UIFont systemFontOfSize:22.f];
    [self.contentView addSubview:_timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@5);
        make.right.equalTo(@-20);
        make.top.equalTo(lab.mas_bottom).offset(5);
        //make.height.equalTo(@50);
    }];
    
    
}
-(void)createDid{
    
    NSArray *labelArr = [NSArray arrayWithObjects:@"获奖用户 :", @"参与人数 :", @"幸运号码 :", @"揭晓时间 :", nil];
    UILabel *lastLabel = nil;
    for (int i = 0; i < 4; i++) {
        UILabel *label = [UILabel new];
        label.font = [UIFont systemFontOfSize:11];
        label.tag = 1000 + i;
        label.text = labelArr[i];
        label.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.qihaoLabel);
            if (lastLabel) {
                
                make.top.mas_equalTo(lastLabel.mas_bottom);
            }else{
                
                make.top.equalTo(self.qihaoLabel.mas_bottom);
            }
            make.width.mas_equalTo(@(SCREEN_WIDTH/2-10));
            make.height.mas_equalTo(@(15));
        }];
        
        lastLabel = label;
    }
    
    
}

- (void)hideDid:(BOOL)hide{
    
    UILabel *nickL = (UILabel *)[self viewWithTag:1000];
    UILabel *userNumbL = (UILabel *)[self viewWithTag:1001];
    UILabel *winNumL = (UILabel *)[self viewWithTag:1002];
    UILabel *timeL = (UILabel *)[self viewWithTag:1003];
    
    [nickL setHidden:hide];
    [userNumbL setHidden:hide];
    [winNumL setHidden:hide];
    [timeL setHidden:hide];
    
}

- (void)hideWill:(BOOL)will{
    UILabel *label = (UILabel *)[self viewWithTag:981];

    [label setHidden:will];
    [_timeLabel setHidden:will];
}

-(void)setNewest:(WH_Home *)newest{
    
    if (_newest != newest) {
        
        _newest = newest;
    }
    
    UILabel *nickL = (UILabel *)[self viewWithTag:1000];
    UILabel *userNumbL = (UILabel *)[self viewWithTag:1001];
    UILabel *winNumL = (UILabel *)[self viewWithTag:1002];
    UILabel *timeL = (UILabel *)[self viewWithTag:1003];
    
    nickL.text = [NSString stringWithFormat:@"获奖用户: %@",_newest.user_nick];
    [WH_AppTools changeColor:nickL.text color:[UIColor blueColor] xx:_newest.user_nick nn:nickL];
    userNumbL.text = [NSString stringWithFormat:@"参与人次: %@",_newest.user_num];
    winNumL.text =  [NSString stringWithFormat:@"幸运号码: %@",_newest.win_num];
    [WH_AppTools changeColor:winNumL.text color:[UIColor redColor] xx:_newest.win_num nn:winNumL];
    timeL.text = [NSString stringWithFormat:@"揭晓时间: %@",_newest.kaijang_time];
    self.qihaoLabel.text = [NSString stringWithFormat:@"期       号: %@",_newest.product_period_no];
    self.picLabel.text = _newest.product_name;
    
    [self.picImage sd_setImageWithURL:[NSURL URLWithString:_newest.product_image] placeholderImage:PLACEHOLDER_CELL_SAME];
    if (_newest.cur_time.length != 0) {
        
        int a = [[WH_AppTools intervalFromLastDate:_newest.cur_time toTheDate:_newest.kaijang_time] intValue];
        if (a > 0) {
            
            [self hideDid:YES];
            [self hideWill:NO];
        }else{
            
            [self hideDid:NO];
            [self hideWill:YES];
        }

    }else{
        
        [self hideDid:NO];
        [self hideWill:YES];
    }
}

- (void)loadData:(id)data indexPath:(NSIndexPath *)indexPath {
    
    if ([data isMemberOfClass:[TimeModel class]]) {
        
        [self storeWeakValueWithData:data indexPath:indexPath];
        
        TimeModel *model = (TimeModel*)data;
        
        _timeLabel.text  = [NSString stringWithFormat:@"%@",[model currentTimeString]];
        //NSLog(@"%@",_timeLabel.text);
        if ([_timeLabel.text isEqualToString:@"00:00:01"]) {
            
            NSLog(@"01000000000000000");
            [self postLotteryD];

        }else if ([[model currentTimeString] isEqualToString:@"00:00:00"]){
            
            if (_newest.cur_time.length != 0) {
                static dispatch_once_t onceToken;
                dispatch_once(&onceToken, ^{
                    
                    [self postLotteryD];

                });
            }
        }

    }
    
}
#pragma mark - 直接开奖获取数据 POST

-(void)postLotteryD{
    
    NSString *body = [NSString stringWithFormat:@"action=lottery&product_period=%@",_newest.product_period];
    
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSLog(@"result = %@", result);
        
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TIME_FRESH object:nil];

    } failure:^(NSError *error) {
        
        
    }];
    
    
}

- (void)storeWeakValueWithData:(id)data indexPath:(NSIndexPath *)indexPath {
    
    self.m_data         = data;
    self.m_tmpIndexPath = indexPath;
}

- (void)dealloc {
    
    NSLog(@"移除cell通知");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_TIME_CELL object:nil];
}

#pragma mark - 通知中心
- (void)registerNSNotificationCenter {
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notificationCenterEvent:)
                                                 name:NOTIFICATION_TIME_CELL
                                               object:nil];
}


- (void)notificationCenterEvent:(id)sender {

    [self loadData:self.m_data indexPath:self.m_tmpIndexPath];
}


@end
